package ecom.nopecom.util;

import org.apache.log4j.Logger;
//import org.apache.log4j.l

import java.util.HashSet;
import java.util.Set;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;
/**
 * Created with IntelliJ IDEA.
 * User: GBY9AMA9
 * Date: 20/04/13
 * Time: 08:32
 * To change this template use File | Settings | File Templates.
 */
public class NopeLogger {
    private final Logger LOGGER;
    private static final Set<String> messageAlreadyLogged = new HashSet<String>();

    public static void initialise() {
        messageAlreadyLogged.clear();
    }

    public NopeLogger(@SuppressWarnings("rawtypes") Class clazz) {
        LOGGER = Logger.getLogger(clazz);
    }

    public static NopeLogger getLogger(@SuppressWarnings("rawtypes") Class clazz){
        return new NopeLogger(clazz);
    }

    /**
     * Log a debug message
     *
     * @param msg
     */
    public void debug(String msg) {
        if (logThisMessage(msg)) LOGGER.debug(msg);
    }

    /**
     * Log debug message (allowing logging of duplicate messages)
     *
     * @param message
     * @param forceLoggingOfDuplicates
     */
    public void debug(String message, boolean forceLoggingOfDuplicates) {
        if (forceLoggingOfDuplicates || logThisMessage(message)) LOGGER.debug(message);
    }

    /**
     * Log a error
     *
     * @param msg
     */
    public void error(String msg) {
        if (logThisMessage(msg)) LOGGER.error(msg);
    }

    /**
     * Log error message (allowing logging of duplicate messages)
     *
     * @param message
     * @param forceLoggingOfDuplicates
     */
    public void error(String message, boolean forceLoggingOfDuplicates) {
        if (forceLoggingOfDuplicates || logThisMessage(message)) LOGGER.error(message);
    }

    /**
     * Log info message
     *
     * @param msg
     */
    public void info(String msg) {
        info(msg, false);
    }

    /**
     * Log info message (allowing logging of duplicate messages)
     *
     * @param message
     * @param forceLoggingOfDuplicates
     */
    public void info(String message, boolean forceLoggingOfDuplicates) {
        if (forceLoggingOfDuplicates || logThisMessage(message)) LOGGER.info(message);
    }

    /**
     * Log warn message
     *
     * @param msg
     */
    public void warn(String msg) {
        if (logThisMessage(msg)) LOGGER.warn(msg);
    }

    public boolean isDebugEnabled() {
        return LOGGER.isDebugEnabled();
    }

    private boolean logThisMessage(String msg) {
        boolean logMessage = !messageAlreadyLogged.contains(msg);
        messageAlreadyLogged.add(msg);
        return logMessage;
    }

}
